package com.naza.rpc.register;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.naza.rpc.client.NacosClient;
import com.naza.rpc.model.ServiceInstance;
import com.naza.rpc.util.InstanceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NacosRegister implements Registry {

    private static final Logger LOG = LoggerFactory.getLogger(NacosRegister.class);

    NacosClient client = NacosClient.getINSTANCE();

    @Override
    public void register(ServiceInstance serviceInstance) {
        Instance instance = InstanceUtil.createInstance(serviceInstance);
        try {
            client.registerInstance(instance.getServiceName(),instance);
        }catch (Exception e){
            LOG.error("Error in register service to nacos.",e);
        }
    }

    @Override
    public void deRegister(ServiceInstance serviceInstance) {
        Instance instance = InstanceUtil.createInstance(serviceInstance);
        try {
            client.deRegisterInstance(instance.getServiceName(),instance);
        }catch (Exception e){
            LOG.error("Error in deRegister service to nacos.",e);
        }
    }
}
